using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SliderBtn : MonoBehaviour
{
    public bool isOn = false;
    private Image image;
    private RectTransform handle;
    private Vector2 pos;

    void Start()
    {
        image = transform.Find("Background").GetComponent<Image>();
        transform.GetComponent<Button>().onClick.AddListener(OnBtnClick);
        handle = transform.Find("Handle Slide Area/Handle").GetComponent<RectTransform>();
        pos = handle.anchoredPosition;

        if (isOn)
        {
            pos.x += 42.9f;
            image.color = StaticData.blue;
            MoveHandle();
        }
    }

    private void OnBtnClick()
    {
        isOn = !isOn;
        image.color = isOn ? StaticData.blue : Color.gray;
        pos.x += isOn ? 42.9f : -42.9f;
        MoveHandle();
    }

    private void MoveHandle()
    {
        handle.anchoredPosition = pos;
    }
}
